#!/bin/bash

#安装go
installGo() {
    echo "指定要安装的go版本:"
    read version
    GOROOT=/home/${user}/dev/go${version}
    GOPATH=/home/${user}/project/golang${version}

    impGoRoot=$(cat /home/${user}/.bash_path | grep GOROOT | head -n 1)
    if [ -z "${impGoRoot}" ]; then
        echo "export GOARCH=amd64" >>/home/${user}/.bash_path
        echo "export GOOS=linux" >>/home/${user}/.bash_path
        echo "export GO111MODULE=on" >>/home/${user}/.bash_path
        echo "export GOPROXY=https://goproxy.io" >>/home/${user}/.bash_path
        #export GOPROXY=https://mirrors.aliyun.com/goproxy/
        echo "" >>/home/${user}/.bash_path
        
    fi

    if [ ! -d ${GOROOT} ]; then
        cd /home/${user}/dev
        wget http://soft.qiniu.anzhongwei.cn/go${version}.linux-amd64.tar.gz
        tar -xvf go${version}.linux-amd64.tar.gz
        mv go go${version}
    fi
        
    sed -i "/export GOROOT/d" /home/${user}/.bash_path
    sed -i "/export GOPATH/d" /home/${user}/.bash_path
    sed -i "/export GOBIN/d" /home/${user}/.bash_path
    sed -i "/\$GOPATH\/bin/d" /home/${user}/.bash_path
    echo "export GOROOT=${GOROOT}" >>/home/${user}/.bash_path
    echo "export GOPATH=${GOPATH}" >>/home/${user}/.bash_path
    echo "export GOBIN=\$GOROOT/bin" >>/home/${user}/.bash_path
    echo "export PATH=\$GOPATH/bin:\$GOROOT/bin:\$PATH" >>/home/${user}/.bash_path
    source /home/${user}/.bash_path

    if [ ! -d /home/${user}/project/golang${version} ]; then
        mkdir -p /home/${user}/project/golang${version}
        mkdir -p /home/${user}/project/golang${version}/bin
        mkdir -p /home/${user}/project/golang${version}/src
        mkdir -p /home/${user}/project/golang${version}/pkg
    fi
    /bin/echo -e "当前GO版本号："
    go version
}

installGo